import random


def probability_judge(trigger_probability, trigger_num=1):
    """
    概率判定函数，如果触发成功则返回True，触发失败则返回False；当trigger_num不等于1时，则进行连续触发判定
    :param trigger_probability: 触发概率，0~1之间的浮点数
    :param trigger_num: 触发次数，默认为一次
    :return:
    """
    for i in range(trigger_num):
        random_number = random.random()
        if random_number <= trigger_probability:
            return True

    return False


if __name__ == '__main__':

    # 单次判定测试
    print("probability_judge测试:")
    res = 0
    test_p = 0.8    # 测试概率
    test_num = 100  # 测试次数
    for i in range(test_num):
        if probability_judge(test_p):
            res += 1
    print("理论触发次数：%.2f，实际触发次数：%.2f" % (test_p * test_num, res))

    # 多次判定测试
    # res = 0
    # for i in range(test_num):
    #     if probability_judge(test_p, 2):
    #         res += 1
    # print("多次触发测试，实际触发次数：%.2f" % res)

